package finedev.android.venue;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.FacebookError;

public class FriendSearchActivity extends Activity {
	
private Activity mActivity = null;
	
	private final String TAG = "friend";
	
	private ProgressDialog progressDialog;
	
//	private Button mBtnFriendAdd = null;
	
	private ListView mListView;
	private FriendSearchAdapter mFriendSearchAdapter;
	private ArrayList<FriendSearchItem> mFriendListData = new ArrayList<FriendSearchItem>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_search);
        
        mActivity = this;
        
        mListView = (ListView) findViewById(R.id.friendsearch_listview);
        mFriendSearchAdapter = new FriendSearchAdapter( mActivity, R.layout.friendsearch_row, mFriendListData );
        mListView.setAdapter( mFriendSearchAdapter );
        
        FriendSearch();
        
    }
    
    private void FriendSearch() {
    	Log.i( TAG, "FriendSearch()");
    	
    	progressDialog = new ProgressDialog( mActivity );
		progressDialog.setMessage("get friend list...");
		progressDialog.setCancelable(false);
		
		progressDialog.show();
		
    	String query = "SELECT uid, name, pic_small, religion FROM user WHERE is_app_user = '1' and uid in ( SELECT uid2 FROM friend WHERE uid1 = me() )";
    	
    	Bundle params = new Bundle();
    	params.putString("method", "fql.query");
    	params.putString("query", query);
    	
    	BasicInfo.FacebookRunnerInstance.request(null, params, new FriendsRequestListener() );

    }
    
    final Handler ListViewUpdateHandler = new Handler()
    {
    	public void handleMessage(android.os.Message msg) {
    		mFriendSearchAdapter.notifyDataSetChanged();
    	}
    };
    
    public class FriendsRequestListener implements RequestListener
    {

    	@Override
        public void onComplete(final String response, final Object state) 
        {
        	JSONObject resultObj = null;
        	JSONArray jArray = null;
			try 
			{
				jArray = new JSONArray( response );
				if ( jArray == null ) return;
				for ( int i=0; i<jArray.length(); i++ ) {
					JSONObject obj = jArray.getJSONObject(i);
					FriendSearchItem friendItem = new FriendSearchItem( obj.getString("uid"), obj.getString("name"), obj.getString("pic_small") );
					mFriendListData.add( friendItem );
				}
				ListViewUpdateHandler.sendEmptyMessage( 0 );
			} catch (FacebookError e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			progressDialog.dismiss();
        }

		@Override
		public void onIOException(IOException e, Object state) {
			// TODO Auto-generated method stub
			progressDialog.dismiss();
		}

		@Override
		public void onFileNotFoundException(FileNotFoundException e,
				Object state) {
			// TODO Auto-generated method stub
			progressDialog.dismiss();
		}

		@Override
		public void onMalformedURLException(MalformedURLException e,
				Object state) {
			// TODO Auto-generated method stub
			progressDialog.dismiss();
		}

		@Override
		public void onFacebookError(FacebookError e, Object state) {
			// TODO Auto-generated method stub
			progressDialog.dismiss();
        	Toast.makeText(getApplicationContext(), "Facebook Error: " + e.getMessage(),
                  Toast.LENGTH_SHORT).show();
		}
    	
    }  
    
}
